package model.caret;

import model.code.*;

import java.util.ArrayList;
import java.util.Collection;

/**
 * This is a representation of the selection of {@link CodeFileChunk}s in {@link editor.headers.Header}s.
 *
 * @author Neil Dickson
 */
public class HeaderCaret extends Caret {
	private ArrayList<CodeFileChunk>	selection;

	public HeaderCaret(CaretRef ref) {
		super(ref);
		selection = new ArrayList<CodeFileChunk>();
	}
	public void add(CodeFileChunk header) {
		if (!contains(header)) {
			selection.add(header);
			if (header instanceof Section) {
				Section section = (Section)header;
				for (CodeFileChunk aChunk : section.getContent()) {
					add(aChunk);
				}
			}
		}
	}
	public boolean contains(CodeFileChunk chunk) {
		return selection.contains(chunk);
	}
	public void addAll(Collection<? extends CodeFileChunk> content) {
		for (CodeFileChunk header : content) {
			add(header);
		}
	}

	public void remove(CodeFileChunk header) {
		selection.remove(header);
		if (header instanceof Section) {
			Section section = (Section)header;
			for (CodeFileChunk aChunk : section.getContent()) {
				remove(aChunk);
			}
		}
	}
	public void removeAllBut(CodeFileChunk header) {
		selection.clear();
		add(header);
	}
	public void toggleSelected(CodeFileChunk header) {
		if (!contains(header)) {
			add(header);
		}
		else {
			remove(header);
		}
	}

	public CodeFileChunk getLast() {
		if (selection.isEmpty()) {
			return null;
		}
		return selection.get(selection.size()-1);
	}

	public ArrayList<CodeFileChunk> getSelection() {
		return selection;
	}
}
